﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TestEntity2
{
    public partial class EditFirm : Form
    {
        int? _ID;
        List<Firm> _list;
        public EditFirm(int? ID , List<Firm> list)
        {
            InitializeComponent();
            _ID = ID;
            _list = list;
        }

        private void EditFirm_Load(object sender, EventArgs e)
        {
            if (_ID != null)
            {
                var fir = _list.Where(i => i.ID == _ID.Value).FirstOrDefault();
                if (fir != null)
                {
                    txbDate.Text = fir.OpenDate.ToShortDateString();
                    txbDiretor.Text = fir.Director;
                    txbName.Text = fir.Name;
                    txbRaiting.Text = fir.Raiting.ToString();
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (_ID != null)
            {
                var fir = _list.Where(i => i.ID == _ID.Value).FirstOrDefault();
                if (fir != null)
                {
                    fir.OpenDate = DateTime.Parse(txbDate.Text);
                    fir.Director = txbDiretor.Text;
                    fir.Name = txbName.Text;
                    fir.Raiting = int.Parse(txbRaiting.Text);
                }
            }
            else
            {

                var h = _list.Count>0?_list.Max(i => i.ID) + 1:0;
                _list.Add(new Firm() { Raiting = int.Parse(txbRaiting.Text), Name = txbName.Text, Director = txbDiretor.Text, OpenDate = DateTime.Parse(txbDate.Text) , ID = h});
            }
            EntityLayer.XmlHelper<Firm> helper = new EntityLayer.XmlHelper<Firm>(FirmEntity.ObjectXmlFile);
            helper.Serialise(_list);
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
